package com.novice.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalTime;

@Component
public class SpringRabbitListener {
    // 1、简单队列模式
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(String message) {
        System.out.println("接收到simple.queue消息：" + message);
    }


    // 提高消息处理进度，避免消息堆积
    // 2、工作队列模式-1[需要进行消息预取]
    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue1(String message) throws InterruptedException {
        System.out.println("1----接收到work.queue消息：" + message + LocalTime.now());
        Thread.sleep(20);
    }

    // 2、工作队列模式-2[需要进行消息预取]
    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue2(String message) throws InterruptedException {
        System.err.println("2---------------接收到work.queue消息：" + message + LocalTime.now());
        Thread.sleep(200);
    }

    // 3、发布订阅模式--》fanout交换机
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String message) throws InterruptedException {
        System.out.println("1---------------接收到fanout.queue1消息：" + message + LocalTime.now());
        Thread.sleep(200);
    }
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String message) throws InterruptedException {
        System.out.println("2---------------接收到fanout.queue2消息：" + message + LocalTime.now());
        Thread.sleep(200);
    }

    static final String EXCHANGE_DIRECT_NAME = "exchange_direct";
    static final String DIRECT_QUEUE_1 = "direct.queue1";
    static final String DIRECT_QUEUE_2 = "direct.queue2";

    // 3、发布订阅模式--》direct交换机
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = DIRECT_QUEUE_1),
            exchange = @Exchange(value = EXCHANGE_DIRECT_NAME, type = ExchangeTypes.DIRECT),
            key = {"blue","red"}
    ))
    public void listenDirectQueue1(String message) throws InterruptedException {
        System.out.println("1-------接收到direct.queue1消息：" + message + LocalTime.now());
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = DIRECT_QUEUE_2),
            exchange = @Exchange(value = EXCHANGE_DIRECT_NAME, type = ExchangeTypes.DIRECT),
            key = {"red","yellow"}
    ))
    public void listenDirectQueue2(String message) throws InterruptedException {
        System.out.println("2---------------接收到direct.queue2消息：" + message + LocalTime.now());
    }

    // 3、发布订阅模式--》topic交换机
    static final String EXCHANGE_TOPIC_NAME = "exchange_topic";
    static final String TOPIC_QUEUE_1 = "topic.queue1";
    static final String TOPIC_QUEUE_2 = "topic.queue2";
    /**
     * # 表示任意通配符
     * . 表示一个通配符
     * @param message 消息
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = TOPIC_QUEUE_1),
            exchange = @Exchange(value = EXCHANGE_TOPIC_NAME, type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void listenTopicQueue(String message) {
        System.out.println("2--------接收到topic.queue1消息：" + message + LocalTime.now());
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = TOPIC_QUEUE_2),
            exchange = @Exchange(value = EXCHANGE_TOPIC_NAME, type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    public void listenTopicQueue2(String message) {
        System.out.println("2---------------接收到topic.queue2消息：" + message + LocalTime.now());
    }
}